package com.weiyun.shortlink.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weiyun.shortlink.admin.biz.user.UserContext;
import com.weiyun.shortlink.admin.common.convention.exception.ServiceException;
import com.weiyun.shortlink.admin.common.convention.result.Result;
import com.weiyun.shortlink.admin.dao.entity.GroupDao;
import com.weiyun.shortlink.admin.dao.mapper.GroupMapper;
import com.weiyun.shortlink.admin.remote.ShortLinkActualRemoteService;
import com.weiyun.shortlink.admin.remote.ShortLinkRemoteService;
import com.weiyun.shortlink.admin.remote.dto.req.ShortLinkRecyclePageReqDto;
import com.weiyun.shortlink.admin.remote.dto.resp.ShortLinkPageRespDto;
import com.weiyun.shortlink.admin.service.RecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/***
 *   @Author:weiyun
 *
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl implements RecycleBinService {
    private final GroupMapper groupMapper;
    ShortLinkRemoteService shortLinkRemoteService = new ShortLinkRemoteService() {};
    private final ShortLinkActualRemoteService shortLinkActualRemoteService;
    @Override
    public Result<Page<ShortLinkPageRespDto>> pageRecycleBin(ShortLinkRecyclePageReqDto requestParam) {
        LambdaQueryWrapper<GroupDao> wrapper = Wrappers.lambdaQuery(GroupDao.class)
                .eq(GroupDao::getUsername, UserContext.getUsername())
                .eq(GroupDao::getDelFlag, 0);
        List<GroupDao> gid = groupMapper.selectList(wrapper);
        if(CollUtil.isEmpty(gid)){
            throw new ServiceException("用户无分组信息");
        }
        requestParam.setGid(gid.stream().map(GroupDao::getGid).toList());
        return shortLinkActualRemoteService.pageRecycleBin(requestParam.getGid(),requestParam.getCurrent(),requestParam.getSize());
    }
}
